using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class PageTree : AdminBaseUserControl
    {
        #region Properties
        public string SelectedValue
        {
            get { return Convert.ToString(ViewState["_ClassID"]); }
            set { ViewState["_ClassID"] = value; }
        }
        #endregion


        public event EventHandler SelectedPageChanged;

        protected void Page_Load(object sender, EventArgs e)
        {
            string target = Request.Form["__EVENTTARGET"];
            if (!string.IsNullOrEmpty(target))
            {
                if (target.Equals(this.tvPage.UniqueID))
                {
                    if (ViewState["previousArg"] != null)
                    {
                        string previousArg = (string)ViewState["previousArg"];
                        if (previousArg.Equals(Request.Form["__EVENTARGUMENT"]))
                        {
                            if (SelectedPageChanged != null && tvPage.SelectedNode.Depth > 0)
                            {
                                this.SelectedValue = tvPage.SelectedValue;
                                SelectedPageChanged(this, new EventArgs());
                            }
                            ViewState["_currArg"] = Request["__EVENTARGUMENT"].ToString();
                        }
                    }
                    ViewState["previousArg"] = Request.Form["__EVENTARGUMENT"];
                }
            }
        }


        public void BindTree(object sender, EventArgs e)
        {
            BindPage();
        }

        public void BindPage()
        {
            tvPage.Nodes.Clear();
            IPage page = PageController.GetPageTree();
            TreeNode nodeRoot = new TreeNode("Pages", "0");
            PopulatePage(page, nodeRoot);
            tvPage.Nodes.Add(nodeRoot);
            tvPage.ExpandAll();

        }

        private void PopulatePage(IPage page, TreeNode node)
        {
            bool add = true;
            if (page.SubPageList != null)
                foreach (IPage p in page.SubPageList)
                { 
                    //for (int i = 0; i < p.ExtPropertyList.Count; i++)
                    //{
                    //    if ((Convert.ToString(p.ExtPropertyList[i].Name) == "TypeClass") && (Convert.ToString(p.ExtPropertyList[i].Value) == "Forum"))
                    //    {
                    //        add = false;
                    //    }
                    //}
                    if (add == true)
                    {
                        TreeNode subNode = new TreeNode(p.Name, p.ID.ToString());
                        node.ChildNodes.Add(subNode);
                        PopulatePage(p, subNode);
                    }
                    else
                        add = false;
                }
        }  

       

        protected void tvPage_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeView tvPage = (TreeView)sender;
            if (SelectedPageChanged != null && tvPage.SelectedNode.Depth> 0)
            {
                this.SelectedValue = tvPage.SelectedValue;
                SelectedPageChanged(this, new EventArgs());
            }
            ViewState["_currArg"] = Request["__EVENTARGUMENT"].ToString();

        }
        
    }
}